﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Vincent._IO
{
    /// <summary>
    /// 解压zip文件
    /// </summary>
    public class UpZip
    {
        /// <summary>
        /// 解压文件
        /// </summary>
        /// <param name="args">包含要解压的文件名和要解压到的目录名数组</param>
        public ArrayList UnZip(string[] args)
        {
            ArrayList arrLst = null;

            //int curIndex = 0;
            //ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));
            //try
            //{
            //    ZipEntry theEntry;
            //    arrLst = new ArrayList();
            //    while ((theEntry = s.GetNextEntry()) != null)
            //    {
            //        string directoryName = Path.GetDirectoryName(args[1]);
            //        string fileName = Path.GetFileName(theEntry.Name);
            //        bool isNew = false;
            //        int length = 0;
            //        //生成解压目录
            //        Directory.CreateDirectory(directoryName);

            //        if (fileName != String.Empty)
            //        {
            //            //解压文件到指定的目录

            //            //string str = args[0].Substring(0, args[0].LastIndexOf("\\") + 1);     //文件夹(E:\\xiangmu\\WebSite2\\UploadFile\image\\)
            //            string[] names = theEntry.Name.Split('.');

            //            //获取当前文件夹下 某类型的文件的个数  并累加获得新文件名字
            //            string[] files = System.IO.Directory.GetFiles(args[0].Substring(0, args[0].LastIndexOf("\\") + 1), "*." + names[names.Length - 1], SearchOption.TopDirectoryOnly);
            //            if (files != null && files.Length >= 1)
            //            {
            //                length = files.Length;
            //                fileName = (length) + "." + names[names.Length - 1];
            //                while (!CompareName(files, fileName))
            //                {
            //                    length++;
            //                    fileName = (length) + "." + names[names.Length - 1];
            //                }
            //            }
            //            else
            //            {
            //                fileName = "0." + names[names.Length - 1];
            //            }
            //            arrLst.Add(fileName);
            //            FileStream streamWriter = File.Create(args[1] + fileName);

            //            int size = 2048;
            //            byte[] data = new byte[2048];
            //            while (true)
            //            {
            //                size = s.Read(data, 0, data.Length);
            //                if (size > 0)
            //                {
            //                    streamWriter.Write(data, 0, size);
            //                }
            //                else
            //                {
            //                    break;
            //                }
            //            }

            //            streamWriter.Close();
            //        }
            //        curIndex++;
            //    }
            //    s.Close();
                return arrLst;
            //}
            //catch (Exception eu)
            //{
            //    throw eu;
            //    return null;
            //}
            //finally
            //{
            //    s.Close();
            //}

        }

        private bool CompareName(string[] files, string fileName)
        {
            bool isNew = true;
            string[] newFiles = null;
            for (int i = 0; i < files.Length; i++)
            {
                newFiles = files[i].Split('\\');

                if (newFiles[newFiles.Length - 1] == fileName)
                {
                    isNew = false;
                    break;
                }
            }
            return isNew;
        }
    }
}
